package com.heima.wemedia.task;

import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmNewsAutoAuditService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @Author:篍
 * @Date: 2025/10/07 20:34
 * @Description:
 */
@Component
@Slf4j
public class WmNewsPublishTask {
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private WmNewsMapper wmNewsMapper;
    @Autowired
    private WmNewsAutoAuditService wmNewsAutoAuditService;

    @Async
    public void newsAutoPublish(){
        log.info("开始执行定时任务");
        RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque("article-deque");
        try {
            while (true){
                //获取文章id
                Integer wmNewsId = (Integer) blockingDeque.poll(10, TimeUnit.SECONDS);
                if (Objects.nonNull(wmNewsId)){
                    WmNews wmNews = wmNewsMapper.selectById(wmNewsId);
                    if (Objects.nonNull(wmNews)){
                        wmNewsAutoAuditService.publishArticle(wmNewsId);
                        log.info("发布文章成功：{}",wmNewsId);
                    }
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
